

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/hello_lixi.gitee.io/img/fluid.png">
  <link rel="icon" href="/hello_lixi.gitee.io/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="Mr.LI">
  <meta name="keywords" content="">
  
    <meta name="description" content="Vue2+Springboot 实现多文件上传,解决 element-ui 在同一个方法中，触发了多个 message 组件提示信息时，会出现消息重叠的问题">
<meta property="og:type" content="article">
<meta property="og:title" content="Vue2+Springboot 实现多文件上传">
<meta property="og:url" content="https://xistudycode.gitee.io/hello_lixi.gitee.io/2023/05/11/Vue+Springboot%E5%A4%9A%E6%96%87%E4%BB%B6%E4%B8%8A%E4%BC%A0/index.html">
<meta property="og:site_name" content="Welcome to the technology blog">
<meta property="og:description" content="Vue2+Springboot 实现多文件上传,解决 element-ui 在同一个方法中，触发了多个 message 组件提示信息时，会出现消息重叠的问题">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://raw.githubusercontent.com/hellelixi/note-image01/main/images/image-20230506145033521.png">
<meta property="og:image" content="https://raw.githubusercontent.com/hellelixi/note-image01/main/images/image-20230425114809873.png">
<meta property="og:image" content="https://raw.githubusercontent.com/hellelixi/note-image01/main/images/image-20230425114914295.png">
<meta property="article:published_time" content="2023-05-11T06:56:07.725Z">
<meta property="article:modified_time" content="2023-05-11T07:10:28.854Z">
<meta property="article:author" content="Mr.LI">
<meta property="article:tag" content="Vue2+Springboot">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://raw.githubusercontent.com/hellelixi/note-image01/main/images/image-20230506145033521.png">
  
  
    <meta name="referrer" content="no-referrer-when-downgrade">
  
  
  <title>Vue2+Springboot 实现多文件上传 - Welcome to the technology blog</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/hello_lixi.gitee.io/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/hello_lixi.gitee.io/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/hello_lixi.gitee.io/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"xistudycode.gitee.io","root":"/hello_lixi.gitee.io/","version":"1.9.3","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":true,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/hello_lixi.gitee.io/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/hello_lixi.gitee.io/js/utils.js" ></script>
  <script  src="/hello_lixi.gitee.io/js/color-schema.js" ></script>
  

  

  

  

  

  

  

  



  
<meta name="generator" content="Hexo 5.4.2"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/hello_lixi.gitee.io/">
      <strong>xBlog</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/hello_lixi.gitee.io/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/hello_lixi.gitee.io/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/hello_lixi.gitee.io/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/hello_lixi.gitee.io/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/hello_lixi.gitee.io/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/hello_lixi.gitee.io/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="Vue2+Springboot 实现多文件上传"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        Mr.LI
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2023-05-11 14:56" pubdate>
          2023年5月11日 下午
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          7.2k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          60 分钟
        
      </span>
    

    
    
      
        <span id="busuanzi_container_page_pv" style="display: none">
          <i class="iconfont icon-eye" aria-hidden="true"></i>
          <span id="busuanzi_value_page_pv"></span> 次
        </span>
        
      
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">Vue2+Springboot 实现多文件上传</h1>
            
              <p class="note note-info">
                
                  
                    本文最后更新于：2023年5月11日 下午
                  
                
              </p>
            
            
              <div class="markdown-body">
                
                <p>Vue+Springboot 多文件上传 </p>
<h3 id="一-参考代码"><a href="#一-参考代码" class="headerlink" title="一. 参考代码"></a>一. 参考代码</h3><p>前端代码：</p>
<p>vue：index.vue</p>
<p>页面代码：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><code class="hljs vue">&lt;!-- 上传文件对话框 --&gt;<br>    &lt;el-dialog :title=&quot;title&quot; :visible.sync=&quot;uploadVideoOpen&quot; width=&quot;500px&quot; append-to-body&gt;<br>      &lt;el-form ref=&quot;form&quot; :model=&quot;form&quot; :rules=&quot;rules&quot; label-width=&quot;90px&quot; style=&quot;text-align:center&quot;&gt;<br>        &lt;el-upload <br>          ref=&quot;uploadVideo&quot;<br>          class=&quot;upload-demo&quot;<br>          drag<br>          action=&quot;&quot;<br>          :limit=&quot;10&quot;<br>          :auto-upload=&quot;false&quot;<br>          :on-change=&quot;uploadOnchange&quot;<br>          :on-remove=&quot;uploadOnRemove&quot;<br>          multiple&gt;<br>          &lt;i class=&quot;el-icon-upload&quot;&gt;&lt;/i&gt;<br>          &lt;div class=&quot;el-upload__text&quot;&gt;将文件拖到此处，或&lt;em&gt;点击上传&lt;/em&gt;&lt;/div&gt;<br>          &lt;div class=&quot;el-upload__tip&quot; slot=&quot;tip&quot;&gt;可以多文件上传&lt;/div&gt;<br>        &lt;/el-upload&gt;<br>        <br>      &lt;/el-form&gt;<br>      &lt;div slot=&quot;footer&quot; class=&quot;dialog-footer&quot;&gt;<br>        &lt;el-button type=&quot;primary&quot; @click=&quot;submitFormUpload&quot;&gt;上 传&lt;/el-button&gt;<br>        &lt;el-button @click=&quot;cancel&quot;&gt;取 消&lt;/el-button&gt;<br>      &lt;/div&gt;<br>    &lt;/el-dialog&gt;<br></code></pre></td></tr></table></figure>



<p>上传方法代码</p>
<p>method{}</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-comment">/** upload on-change事件 */</span><br><span class="hljs-title function_">uploadOnchange</span>(<span class="hljs-params">file, fileList</span>)&#123;<br>  <span class="hljs-variable language_">this</span>.<span class="hljs-property">videoFileList</span> = fileList;<br>&#125;,<br><br><span class="hljs-comment">/** upload on-remove事件 */</span><br><span class="hljs-title function_">uploadOnRemove</span>(<span class="hljs-params">file, fileList</span>)&#123;<br>  <span class="hljs-variable language_">this</span>.<span class="hljs-property">videoFileList</span> = fileList;<br>&#125;,<br><br><span class="hljs-comment">/** 上传视频到后台 */</span><br><span class="hljs-title function_">submitFormUpload</span>(<span class="hljs-params"></span>)&#123;<br>  <span class="hljs-keyword">const</span> appUserId = <span class="hljs-variable language_">this</span>.<span class="hljs-property">uploadVideoUserId</span>;<br>  <span class="hljs-keyword">const</span> videoFiles = <span class="hljs-variable language_">this</span>.<span class="hljs-property">videoFileList</span>;<br>  <span class="hljs-keyword">let</span> formData = <span class="hljs-keyword">new</span> <span class="hljs-title class_">FormData</span>();<br>  <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> file <span class="hljs-keyword">of</span> videoFiles) &#123;<br>    formData.<span class="hljs-title function_">append</span>(<span class="hljs-string">&quot;videoFiles&quot;</span>, file.<span class="hljs-property">raw</span>);<br>  &#125;<br>  <span class="hljs-variable language_">this</span>.<span class="hljs-property">$modal</span>.<span class="hljs-title function_">msgSuccess</span>(<span class="hljs-string">&quot;上传中请耐心等待，可操作其他&quot;</span>);<br>  <span class="hljs-title function_">uploadVideo</span>(appUserId, formData).<span class="hljs-title function_">then</span>(<span class="hljs-function"><span class="hljs-params">response</span> =&gt;</span> &#123;<br>    <span class="hljs-variable language_">this</span>.<span class="hljs-property">$modal</span>.<span class="hljs-title function_">msgSuccess</span>(<span class="hljs-string">&quot;上传视频成功&quot;</span>);<br>    <span class="hljs-variable language_">this</span>.<span class="hljs-property">uploadVideoUserId</span> = <span class="hljs-literal">null</span>;<br>    <span class="hljs-comment">// 清空上传file列表参数</span><br>    <span class="hljs-variable language_">this</span>.<span class="hljs-property">$refs</span>.<span class="hljs-property">uploadVideo</span>.<span class="hljs-title function_">clearFiles</span>();<br>    <span class="hljs-variable language_">this</span>.<span class="hljs-property">uploadVideoOpen</span> = <span class="hljs-literal">false</span>;<br>    <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">getList</span>();<br>   &#125;);<br>&#125;,<br></code></pre></td></tr></table></figure>

<p>导出函数</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs js">  <span class="hljs-comment">//文件上传</span><br><span class="hljs-keyword">export</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">uploadFile</span>(<span class="hljs-params">deviceId,formData</span>) &#123;<br>    <span class="hljs-keyword">return</span> <span class="hljs-title function_">request</span>(&#123;<br>      <span class="hljs-attr">url</span>: <span class="hljs-string">&#x27;/expt/DeviceManage/uploadFile/&#x27;</span>+deviceId,<br>      <span class="hljs-attr">method</span>: <span class="hljs-string">&#x27;post&#x27;</span>,<br>        <span class="hljs-comment">//需要添加请求头</span><br>      <span class="hljs-attr">headers</span>: &#123;<br>        <span class="hljs-string">&#x27;Content-Type&#x27;</span>: <span class="hljs-string">&#x27;multipart/form-data&#x27;</span><br>      &#125;,<br>      <span class="hljs-attr">data</span>: formData<br>    &#125;)<br>&#125;<br><br></code></pre></td></tr></table></figure>





<p>后端代码</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 上传视频</span><br><span class="hljs-comment"> */</span><br><span class="hljs-meta">@PostMapping(&quot;/uploadVideo/&#123;appUserId&#125;&quot;)</span><br><span class="hljs-keyword">public</span> AjaxResult <span class="hljs-title function_">uploadVideo</span><span class="hljs-params">(<span class="hljs-meta">@PathVariable(&quot;appUserId&quot;)</span> Long appUserId,</span><br><span class="hljs-params">                              <span class="hljs-meta">@RequestParam(&quot;videoFiles&quot;)</span> MultipartFile[] videoFiles)</span> &#123;<br>    <br>    <span class="hljs-keyword">if</span> (StringUtils.isNotEmpty(videoFiles)) &#123;<br>        <span class="hljs-comment">//遍历文件</span><br>        <span class="hljs-keyword">for</span> (MultipartFile multipartFile : videoFiles) &#123;<br>            <span class="hljs-keyword">if</span>(!multipartFile.isEmpty())&#123;<br>                <span class="hljs-keyword">try</span> &#123;<br>                    <span class="hljs-type">String</span> <span class="hljs-variable">fileName</span> <span class="hljs-operator">=</span> multipartFile.getOriginalFilename();<br>                    fileName =  fileName.substring(<span class="hljs-number">0</span>, fileName.lastIndexOf(<span class="hljs-string">&quot;.&quot;</span>));<br>                    System.out.println(<span class="hljs-string">&quot;文件名：&quot;</span> + fileName);<br>                    Map&lt;String, String&gt; mapResult = MinioUtils.putObjectStreamMapResult(multipartFile, <span class="hljs-string">&quot;yifan-video&quot;</span>);<br><br>                &#125; <span class="hljs-keyword">catch</span> (Exception e) &#123;<br>                    e.printStackTrace();<br>                    log.error(<span class="hljs-string">&quot;上传到minio失败&quot;</span>, e);<br>                &#125;<br>            &#125;<br>        &#125;<br>    &#125;<br>    <span class="hljs-keyword">return</span> AjaxResult.success();<br>&#125;<br></code></pre></td></tr></table></figure>



<h3 id="二-自己项目代码-yeyajian"><a href="#二-自己项目代码-yeyajian" class="headerlink" title="二. 自己项目代码(yeyajian)"></a>二. 自己项目代码(yeyajian)</h3><p>vue</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><code class="hljs vue">&lt;el-form ref=&quot;form&quot; :model=&quot;form&quot; :rules=&quot;rules&quot; label-width=&quot;90px&quot; style=&quot;text-align:center&quot;&gt;<br>  &lt;el-upload<br>    ref=&quot;uploadFiles&quot;<br>    class=&quot;upload-demo&quot;<br>    drag<br>    action=&quot;&quot;<br>    :limit=&quot;10&quot;<br>    :auto-upload=&quot;false&quot;<br>    :on-change=&quot;uploadOnchange&quot;<br>    :on-remove=&quot;uploadOnRemove&quot;<br>    multiple&gt;<br>    &lt;i class=&quot;el-icon-upload&quot;&gt;&lt;/i&gt;<br>    &lt;div class=&quot;el-upload__text&quot;&gt;设备附件：将文件拖到此处，或&lt;em&gt;点击上传&lt;/em&gt;&lt;/div&gt;<br>    &lt;div class=&quot;el-upload__tip&quot; slot=&quot;tip&quot;&gt;可以多文件上传，每个文件大小不超过1M&lt;/div&gt;<br>    &lt;div class=&quot;el-upload__tip&quot; slot=&quot;tip&quot;&gt;文件类型 ( bmp, gif, jpg, jpeg, png,<br>      doc, docx, xls, xlsx, ppt, pptx, html, htm, txt,<br>      rar, zip, gz, bz2,<br>      mp4, avi, rmvb,<br>      pdf ) &lt;/div&gt;<br>  &lt;/el-upload&gt;<br>&lt;/el-form&gt;<br></code></pre></td></tr></table></figure>



<p>methos</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-comment">/** upload on-change事件 */</span><br><span class="hljs-title function_">uploadOnchange</span>(<span class="hljs-params">file, fileList</span>)&#123;<br>  <span class="hljs-comment">/** 限制上传文件大小 */</span><br>  <span class="hljs-keyword">if</span>(file.<span class="hljs-property">size</span> &gt; <span class="hljs-variable language_">this</span>.<span class="hljs-property">FileSizeLimit</span>) &#123;<br>    fileList.<span class="hljs-title function_">pop</span>();<br>    <span class="hljs-comment">//this.$message.error(`文件 &quot;$&#123;file.name&#125;&quot; 大小不能超过 $&#123;this.FileSizeLimit / 1024 / 1024&#125; MB`);</span><br>    <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =&gt;</span> &#123;<br>      <span class="hljs-variable language_">this</span>.<span class="hljs-property">$message</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">`文件 &quot;<span class="hljs-subst">$&#123;file.name&#125;</span>&quot; 大小不能超过 <span class="hljs-subst">$&#123;<span class="hljs-variable language_">this</span>.FileSizeLimit / <span class="hljs-number">1024</span> / <span class="hljs-number">1024</span>&#125;</span> MB`</span>);<br>    &#125;, <span class="hljs-number">5</span>);<br>  &#125;<br>  <span class="hljs-comment">/** 限制上传文件类型 */</span><br>  <span class="hljs-keyword">const</span> fileType = file.<span class="hljs-property">name</span>.<span class="hljs-title function_">slice</span>(file.<span class="hljs-property">name</span>.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-string">&#x27;.&#x27;</span>) + <span class="hljs-number">1</span>).<span class="hljs-title function_">toLowerCase</span>();<br>  <span class="hljs-keyword">if</span> (!<span class="hljs-variable language_">this</span>.<span class="hljs-property">allowedFileTypes</span>.<span class="hljs-title function_">has</span>(fileType)) &#123;<br>    fileList.<span class="hljs-title function_">pop</span>();<br>    <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =&gt;</span> &#123;<br>      <span class="hljs-variable language_">this</span>.<span class="hljs-property">$message</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">`文件 &quot;<span class="hljs-subst">$&#123;file.name&#125;</span>&quot; 类型不符合要求`</span>);<br>    &#125;, <span class="hljs-number">5</span>);<br>    <span class="hljs-comment">//this.$message.error(`文件 &quot;$&#123;file.name&#125;&quot; 类型不符合要求`);</span><br>  &#125;<br>  <span class="hljs-variable language_">this</span>.<span class="hljs-property">attachFileList</span> = fileList;<br>&#125;,<br><br><span class="hljs-comment">/** upload on-remove事件 */</span><br><span class="hljs-title function_">uploadOnRemove</span>(<span class="hljs-params">file, fileList</span>)&#123;<br>  <span class="hljs-variable language_">this</span>.<span class="hljs-property">attachFileList</span> = fileList;<br>&#125;,<br><br><span class="hljs-comment">/** 提交按钮 */</span><br><span class="hljs-title function_">submitForm</span>(<span class="hljs-params"></span>) &#123;<br>  <span class="hljs-variable language_">this</span>.<span class="hljs-property">$refs</span>[<span class="hljs-string">&quot;form&quot;</span>].<span class="hljs-title function_">validate</span>(<span class="hljs-function"><span class="hljs-params">valid</span> =&gt;</span> &#123;<br>    <span class="hljs-keyword">if</span> (valid) &#123;<br>      <span class="hljs-title function_">addDeviceManage</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">form</span>).<span class="hljs-title function_">then</span>(<span class="hljs-function"><span class="hljs-params">response</span> =&gt;</span> &#123;<br>        <span class="hljs-comment">/** 500代表后台返回的错误信息,这里判断deviceId是不是已经存在 */</span><br>        <span class="hljs-keyword">if</span> (response.<span class="hljs-property">code</span> == <span class="hljs-number">500</span>) &#123;<br>          <span class="hljs-variable language_">this</span>.<span class="hljs-property">$modal</span>.<span class="hljs-title function_">msgError</span>(response.<span class="hljs-property">msg</span>);<br>          <span class="hljs-keyword">return</span>;<br>        &#125;<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(response.<span class="hljs-property">code</span> == <span class="hljs-number">200</span>)&#123;<br>          <span class="hljs-variable language_">this</span>.<span class="hljs-property">$modal</span>.<span class="hljs-title function_">msgSuccess</span>(<span class="hljs-string">&quot;新增成功&quot;</span>);<br>          <span class="hljs-comment">/** 附件上传 */</span><br>          <span class="hljs-keyword">let</span> deviceId = <span class="hljs-variable language_">this</span>.<span class="hljs-property">form</span>.<span class="hljs-property">deviceId</span>;<br>          <span class="hljs-keyword">const</span> attacheFiles = <span class="hljs-variable language_">this</span>.<span class="hljs-property">attachFileList</span>;<br>          <span class="hljs-keyword">let</span> formData = <span class="hljs-keyword">new</span> <span class="hljs-title class_">FormData</span>();<br>          <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> file <span class="hljs-keyword">of</span> attacheFiles) &#123;<br>            formData.<span class="hljs-title function_">append</span>(<span class="hljs-string">&quot;files&quot;</span>, file.<span class="hljs-property">raw</span>);<br>          &#125;<br>          <span class="hljs-title function_">uploadFile</span>(deviceId, formData).<span class="hljs-title function_">then</span>(<span class="hljs-function"><span class="hljs-params">response</span> =&gt;</span> &#123;<br>            <span class="hljs-variable language_">this</span>.<span class="hljs-property">$modal</span>.<span class="hljs-title function_">msgSuccess</span>(<span class="hljs-string">&quot;上传附件成功&quot;</span>);<br>            <span class="hljs-comment">// 清空上传file列表参数</span><br>            <span class="hljs-variable language_">this</span>.<span class="hljs-property">$refs</span>.<span class="hljs-property">uploadFiles</span>.<span class="hljs-title function_">clearFiles</span>();<br>          &#125;);<br>          <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">getList</span>();<br>          <span class="hljs-variable language_">this</span>.<span class="hljs-property">open</span> = <span class="hljs-literal">false</span>;<br>        &#125;<br>      &#125;);<br>    &#125;<br>  &#125;);<br>&#125;,<br><br></code></pre></td></tr></table></figure>

<p>注意：这里有个问题，使用element-ui，在同一个方法中，触发了多个 message 组件提示信息时，会出现消息重叠的问题，因此不能直接使用</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-variable language_">this</span>.<span class="hljs-property">$message</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">`文件 &quot;<span class="hljs-subst">$&#123;file.name&#125;</span>&quot; 类型不符合要求`</span>);<br></code></pre></td></tr></table></figure>

<p>需要通过setTimeout延迟一点推送的时间</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =&gt;</span> &#123;<br>    <span class="hljs-variable language_">this</span>.<span class="hljs-property">$message</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">`文件 &quot;<span class="hljs-subst">$&#123;file.name&#125;</span>&quot; 大小不能超过 <span class="hljs-subst">$&#123;<span class="hljs-variable language_">this</span>.FileSizeLimit / <span class="hljs-number">1024</span> / <span class="hljs-number">1024</span>&#125;</span> MB`</span>);<br> &#125;, <span class="hljs-number">5</span>);<br></code></pre></td></tr></table></figure>

<p><img src="https://raw.githubusercontent.com/hellelixi/note-image01/main/images/image-20230506145033521.png" srcset="/hello_lixi.gitee.io/img/loading.gif" lazyload alt="image-20230506145033521"></p>
<p>后端</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 设备附件多文件上传</span><br><span class="hljs-comment"> */</span><br><span class="hljs-meta">@RequiresPermissions(&quot;expt:DeviceManage:add&quot;)</span><br><span class="hljs-meta">@PostMapping(&quot;/uploadFile/&#123;deviceId&#125;&quot;)</span><br><span class="hljs-keyword">public</span> AjaxResult <span class="hljs-title function_">uploadFile</span><span class="hljs-params">(<span class="hljs-meta">@PathVariable(value = &quot;deviceId&quot;)</span> Long deviceId, <span class="hljs-meta">@RequestParam(value = &quot;files&quot;)</span> MultipartFile[] files)</span> <span class="hljs-keyword">throws</span> IOException &#123;<br>    <span class="hljs-comment">//遍历文件数组</span><br>    <span class="hljs-keyword">if</span> (StringUtils.isNotEmpty(files)) &#123;<br>        <span class="hljs-keyword">for</span> (MultipartFile multipartFile : files) &#123;<br>            <span class="hljs-comment">//判断文件是否为空</span><br>            <span class="hljs-comment">//if (!multipartFile.isEmpty()) &#123;</span><br>            <span class="hljs-keyword">try</span> &#123;<br>                <span class="hljs-comment">//上传单文件</span><br>                R&lt;SysFile&gt; r = remoteFileService.upload(multipartFile);<br>                <span class="hljs-keyword">if</span> (r.getCode() == <span class="hljs-number">200</span>) &#123;<br>                    <span class="hljs-type">ExpDevAttach</span> <span class="hljs-variable">attachment</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ExpDevAttach</span>();<br>                    String fileName= r.getData().getName();<br>                    <span class="hljs-type">String</span> <span class="hljs-variable">extension</span> <span class="hljs-operator">=</span> org.apache.commons.io.FilenameUtils.getExtension(fileName);<br>                    attachment.setName(fileName);<br>                    attachment.setUrl(r.getData().getUrl());<br>                    attachment.setType(extension);<br>                    attachment.setDevId(deviceId);<br>                    expDevAttachService.insertExpDevAttach(attachment);<br>                &#125;<br>            &#125; <span class="hljs-keyword">catch</span> (Exception e) &#123;<br>                    e.printStackTrace();<br>                    logger.error(<span class="hljs-string">&quot;上传文件失败&quot;</span>, e);<br>            &#125;<br><br>            <span class="hljs-comment">//&#125;</span><br><br>        &#125;<br>    &#125;<br><br>    <span class="hljs-keyword">return</span> AjaxResult.success();<br><br>&#125;<br></code></pre></td></tr></table></figure>





<h3 id="三-遇到的问题"><a href="#三-遇到的问题" class="headerlink" title="三. 遇到的问题"></a>三. 遇到的问题</h3><p>1.Required request part ‘file’ is not present</p>
<p>在上传文件的时候报错 <strong>Required request part ‘file’ is not present</strong>（所需的请求部分“文件”不存在）<br>其中一个原因就是获取上传文件的名字不唯一</p>
<p>后端的参数要和前端formdata塞进去的参数一致</p>
<p><img src="https://raw.githubusercontent.com/hellelixi/note-image01/main/images/image-20230425114809873.png" srcset="/hello_lixi.gitee.io/img/loading.gif" lazyload alt="image-20230425114809873"></p>
<p>vue</p>
<p><img src="https://raw.githubusercontent.com/hellelixi/note-image01/main/images/image-20230425114914295.png" srcset="/hello_lixi.gitee.io/img/loading.gif" lazyload alt="image-20230425114914295"></p>
<ol start="2">
<li>使用element-ui，在同一个方法中，触发了多个 message 组件提示信息时，会出现消息重叠的问题，因此不能直接使用，解决办法使用setTimeout(）延迟显示。</li>
</ol>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/hello_lixi.gitee.io/categories/java%E5%BC%80%E5%8F%91/" class="category-chain-item">java开发</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/hello_lixi.gitee.io/tags/Vue2-Springboot/">#Vue2+Springboot</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>Vue2+Springboot 实现多文件上传</div>
      <div>https://xistudycode.gitee.io/hello_lixi.gitee.io/2023/05/11/Vue+Springboot多文件上传/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>Mr.LI</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2023年5月11日</div>
        </div>
      
      
      
        <div class="license-meta-item">
          <div>许可协议</div>
          <div>
            
              
              
                <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
                  <span class="hint--top hint--rounded" aria-label="BY - 署名">
                    <i class="iconfont icon-by"></i>
                  </span>
                </a>
              
            
          </div>
        </div>
      
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/hello_lixi.gitee.io/2023/05/11/RuoYi%E8%87%AA%E5%8A%A8%E4%BB%A3%E7%A0%81%E7%94%9F%E6%88%90%E5%8A%9F%E8%83%BD%E7%9A%84%E4%BD%BF%E7%94%A8/" title="RuoYi系统自动生成代码功能的使用">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">RuoYi系统自动生成代码功能的使用</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/hello_lixi.gitee.io/2023/05/11/docker-%E5%AE%B9%E5%99%A8%E7%9A%84%E5%AE%89%E8%A3%85%E4%BD%BF%E7%94%A8/" title="Docker容器的安装使用">
                        <span class="hidden-mobile">Docker容器的安装使用</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>xBolg</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>@Fluid</span></a> 
    </div>
  
  
    <div class="statistics">
  
  

  
    
      <span id="busuanzi_container_site_pv" style="display: none">
        总访问量 
        <span id="busuanzi_value_site_pv"></span>
         次
      </span>
    
    
      <span id="busuanzi_container_site_uv" style="display: none">
        总访客数 
        <span id="busuanzi_value_site_uv"></span>
         人
      </span>
    
    
  
</div>

  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/hello_lixi.gitee.io/js/events.js" ></script>
<script  src="/hello_lixi.gitee.io/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/hello_lixi.gitee.io/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init(Object.assign({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      scrollSmooth    : true,
      includeTitleTags: true,
      headingsOffset  : -boardTop,
    }, CONFIG.toc));
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }

    Fluid.events.registerRefreshCallback(function() {
      if ('tocbot' in window) {
        tocbot.refresh();
        var toc = jQuery('#toc');
        if (toc.length === 0 || !tocbot) {
          return;
        }
        if (toc.find('.toc-list-item').length > 0) {
          toc.css('visibility', 'visible');
        }
      }
    });
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.11/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));

    Fluid.events.registerRefreshCallback(function() {
      if ('anchors' in window) {
        anchors.removeAll();
        var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
        var res = [];
        for (var item of el) {
          res.push('.markdown-body > ' + item.trim());
        }
        if (CONFIG.anchorjs.placement === 'left') {
          anchors.options.class = 'anchorjs-link-left';
        }
        anchors.add(res.join(', '));
      }
    });
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/hello_lixi.gitee.io/js/local-search.js" ></script>

  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/hello_lixi.gitee.io/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
